<template>
	<view class="pwd-retrieve-container">
		<uni-forms ref="form" :value="user" labelWidth="80px">
			<uni-forms-item name="oldPassword" :label="this.$t('mine.pwd.oldPwd')" style="color: white;">
				<uni-easyinput type="password" v-model="user.oldPassword"
					:placeholder="this.$t('mine.pwd.enterOldPwd')" />
			</uni-forms-item>
			<uni-forms-item name="newPassword" :label="this.$t('mine.pwd.newPwd')">
				<uni-easyinput type="password" v-model="user.newPassword"
					:placeholder="this.$t('mine.pwd.enterNewPwd')" />
			</uni-forms-item>
			<uni-forms-item name="confirmPassword" :label="this.$t('mine.pwd.confirmPwd')">
				<uni-easyinput type="password" v-model="user.confirmPassword"
					:placeholder="this.$t('mine.pwd.enterConfirmPwd')" />
			</uni-forms-item>
			<button type="primary" style="border-radius: 20px;background-color: #F6DDA4; color: #21242D;"
				@click="submit">{{$t('common.submit')}}</button>
		</uni-forms>
	</view>
</template>

<script>
	import {
		updateUserPwd
	} from "@/api/system/user"

	export default {
		data() {
			return {
				user: {
					oldPassword: undefined,
					newPassword: undefined,
					confirmPassword: undefined
				},
				rules: {
					oldPassword: {
						rules: [{
							required: true,
							errorMessage: this.$t('mine.pwd.nullOldPwd')
						}]
					},
					newPassword: {
						rules: [{
								required: true,
								errorMessage: this.$t('mine.pwd.nullNewPwd'),
							},
							{
								minLength: 6,
								maxLength: 20,
								errorMessage: this.$t('mine.pwd.length')
							}
						]
					},
					confirmPassword: {
						rules: [{
							required: true,
							errorMessage: this.$t('mine.pwd.nullConfirmPwd')
						}, {
							validateFunction: (rule, value, data) => data.newPassword === value,
							errorMessage: this.$t('mine.pwd.diffPwd')
						}]
					}
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			submit() {
				this.$refs.form.validate().then(res => {
					updateUserPwd(this.user.oldPassword, this.user.newPassword).then(response => {
						this.$modal.msgSuccess(this.$t('common.modifySuccess'))
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $uni-color-bg;

	}

	.pwd-retrieve-container {
		padding-top: 36rpx;
		padding: 15px;
	}
</style>